package com.glela.common.util;

import java.text.StringCharacterIterator;
import java.util.ArrayList;

/**
 * 
 *
 * @author liujie
 * 
 */
public class StringExUtil {
    private String m_string = "";

    public StringExUtil(String string) {
        m_string = string;
    }

    public StringExUtil(int i) {
        m_string = String.valueOf(i);
    }

    public StringExUtil(float f) {
        m_string = String.valueOf(f);
    }

    public StringExUtil(double f) {
        m_string = String.valueOf(f);
    }

    public StringExUtil(char f) {
        m_string = String.valueOf(f);
    }

    public StringExUtil(Object o) {
        m_string = String.valueOf(o);
    }

    public StringExUtil(char[] c) {
        m_string = String.valueOf(c);
    }

    public char charAt(int c) {
        if (m_string == null || c < 0 || c >= m_string.length()) {
            return ' ';
        }
        return m_string.charAt(c);
    }

    public boolean equals(Object o) {
        if (m_string == null) {
            return o == null;
        }
        return m_string.equals(o);
    }

    public boolean equalsIgnoreCase(String anotherString) {
        if (m_string == null) {
            return anotherString == null;
        }
        return m_string.equalsIgnoreCase(anotherString);
    }

    public byte[] getBytes() {
        if (m_string == null) {
            return new byte[0];
        }
        return m_string.getBytes();
    }

    public int indexOf(int c, int start) {
        if (m_string == null) {
            return -1;
        }
        return m_string.indexOf(c, start);
    }

    public int indexOf(int c) {
        if (m_string == null) {
            return -1;
        }
        return m_string.indexOf(c);
    }

    public int indexOf(String s, int start) {
        if (m_string == null) {
            return -1;
        }
        return m_string.indexOf(s, start);
    }

    public int indexOf(String s) {
        if (m_string == null || s == null) {
            return -1;
        }
        return m_string.indexOf(s);
    }

    public int lastIndexOf(int c, int start) {
        if (m_string == null) {
            return -1;
        }
        return m_string.lastIndexOf(c, start);
    }

    public int lastIndexOf(int c) {
        if (m_string == null) {
            return -1;
        }
        return m_string.lastIndexOf(c);
    }

    public int lastIndexOf(String s, int start) {
        if (m_string == null || s == null) {
            return -1;
        }
        return m_string.lastIndexOf(s, start);
    }

    public int lastIndexOf(String s) {
        if (m_string == null || s == null) {
            return -1;
        }
        return m_string.lastIndexOf(s);
    }

    public String toString() {
        return m_string == null ? "" : m_string;
    }

    public int length() {
        if (m_string == null) {
            return 0;
        }
        return m_string.length();
    }

    public String trim() {
        if (m_string == null) {
            return "";
        }
        return m_string.trim();
    }

    public String toUpperCase() {
        if (m_string == null) {
            return "";
        }
        return m_string.toUpperCase();
    }

    public String toLowerCase() {
        if (m_string == null) {
            return "";
        }
        return m_string.toLowerCase();
    }

    public int[] countChar(char c) {
        if (m_string == null) {
            return new int[0];
        }

        ArrayList<Integer> v = new ArrayList<Integer>(m_string.length());
        int pos;
        int pos1 = 0;
        pos = m_string.indexOf(c, pos1);
        while (pos != -1) {
            v.add(new Integer(pos));
            pos1 = pos + 1;
            pos = m_string.indexOf(c, pos1);
        }
        int[] ret = new int[v.size()];
        for (int k = 0; k < v.size(); ++k) {
            ret[k] = ((Integer) (v.get(k))).intValue();
        }
        return ret;
    }

    /**
     * 
     *
     * @param flag
     *
     * @return String[]
     */
    public String[] toStringArray(char flag) {
        if (m_string == null) {
            return new String[0];
        }

        String[] sList = m_string.split(forRegex(String.valueOf(flag)));
        ArrayList<String> sArray = new ArrayList<String>(sList.length);
        for (int i = 0; i < sList.length; ++i) {
            if (sList[i].length() != 0) {
                sArray.add(sList[i]);
            }
        }

        return (String[]) sArray.toArray(new String[sArray.size()]);
    }

    public String[] toStringArrayEx(char flag) {
        if (m_string == null) {
            return new String[0];
        }

        return m_string.split(forRegex(String.valueOf(flag)));
    }

    /**
     * Replace characters having special meaning in regular expressions
     * with their escaped equivalents.
     * <p/>
     * <P>The escaped characters include :
     * <ul>
     * <li>.
     * <li>\
     * <li>?, * , and +
     * <li>&
     * <li>:
     * <li>{ and }
     * <li>[ and ]
     * <li>( and )
     * <li>^ and $
     * </ul>
     */
    public static String forRegex(String aRegexFragment) {
        final StringBuffer result = new StringBuffer();

        final StringCharacterIterator iterator = new StringCharacterIterator(aRegexFragment);
        char character = iterator.current();
        while (character != StringCharacterIterator.DONE) {
            /*
            * All literals need to have backslashes doubled.
            */
            if (character == '.') {
                result.append("\\.");
            } else if (character == '\\') {
                result.append("\\\\");
            } else if (character == '?') {
                result.append("\\?");
            } else if (character == '*') {
                result.append("\\*");
            } else if (character == '+') {
                result.append("\\+");
            } else if (character == '&') {
                result.append("\\&");
            } else if (character == ':') {
                result.append("\\:");
            } else if (character == '{') {
                result.append("\\{");
            } else if (character == '}') {
                result.append("\\}");
            } else if (character == '[') {
                result.append("\\[");
            } else if (character == ']') {
                result.append("\\]");
            } else if (character == '(') {
                result.append("\\(");
            } else if (character == ')') {
                result.append("\\)");
            } else if (character == '^') {
                result.append("\\^");
            } else if (character == '$') {
                result.append("\\$");
            } else if (character == '|') {
                result.append("\\|");
            } else {
                //the char is not a special one
                //add it to the result as is
                result.append(character);
            }
            character = iterator.next();
        }
        return result.toString();
    }

    public String replace(String source, String target, boolean flag) {
        if (m_string == null) {
            return "";
        }
        if (source == null || target == null) {
            return m_string;
        }

        return m_string.replaceAll(forRegex(source), target);
    }

    public String strToData() {
        if (m_string == null) {
            return "";
        }
        return m_string.replaceAll("'", "''");
    }

    public String dataToStr() {
        if (m_string == null) {
            return "";
        }
        return m_string.replaceAll("\"", "\\\\\"").replaceAll("'", "\\\\\'");
    }

    public static String dataToStr(String string) {
        if (string == null) {
            return "";
        }
        return string.replaceAll("\"", "\\\\\"").replaceAll("'", "\\\\\'");
    }

    public static String strToData(String string) {
        if (string == null) {
            return "";
        }
        return string.replaceAll("'", "''");
    }

    public static String replace(String string, String source, String target) {
        if (string == null) {
            return "";
        }
        if (source == null || target == null) {
            return string;
        }
        return string.replaceAll(forRegex(source), target);
    }

   
    public static String[] toStringArray(String string, char flag) {
        if (string == null) {
            return new String[0];
        }

        String[] sList = string.split(forRegex(String.valueOf(flag)));
        ArrayList<String> sArray = new ArrayList<String>(sList.length);
        for (int i = 0; i < sList.length; ++i) {
            if (sList[i].length() != 0) {
                sArray.add(sList[i]);
            }
        }

        return (String[]) sArray.toArray(new String[sArray.size()]);
    }

    public static String[] toStringArrayEx(String string, char flag) {
        if (string == null) {
            return new String[0];
        }

        return string.split(forRegex(String.valueOf(flag)));
    }

    public static int[] countChar(String string, char c) {
        if (string == null) {
            return new int[0];
        }

        ArrayList<Integer> v = new ArrayList<Integer>(string.length());
        int pos;
        int pos1 = 0;
        pos = string.indexOf(c, pos1);
        while (pos != -1) {
            v.add(new Integer(pos));
            pos1 = pos + 1;
            pos = string.indexOf(c, pos1);
        }
        int[] ret = new int[v.size()];
        for (int k = 0; k < v.size(); ++k) {
            ret[k] = ((Integer) (v.get(k))).intValue();
        }
        return ret;
    }

    public static int[] toIntArray(String[] sArray) {
        if (sArray == null) {
            return new int[0];
        }

        int[] ai = new int[sArray.length];
        for (int i = 0; i < sArray.length; ++i) {
            ai[i] = ExtendUtil.strToInteger(sArray[i]).intValue();
        }
        return ai;
    }

    public static int inArray(int value, int[] ilist) {
        if (ilist == null) {
            return -1;
        }

        for (int i = 0; i < ilist.length; ++i) {
            if (ilist[i] == value) {
                return i;
            }
        }
        return -1;
    }

    public static int inArray(String value, String[] slist) {
        if (slist == null) {
            return -1;
        }
        for (int i = 0; i < slist.length; ++i) {
            if (slist[i].equals(value)) {
                return i;
            }
        }
        return -1;
    }

    public static String[] toStringArray(int[] ia) {
        if (ia == null) {
            return new String[0];
        }
        String[] str = new String[ia.length];
        for (int i = 0; i < ia.length; ++i) {
            str[i] = String.valueOf(ia[i]);
        }
        return str;
    }

    public static String[] arrayAddArray(String[] str1, String[] str2) {
        if (str1 == null) {
            return str2;
        }
        if (str2 == null) {
            return str1;
        }

        String[] newstr = new String[str1.length + str2.length];
        System.arraycopy(str1, 0, newstr, 0, str1.length);
        System.arraycopy(str2, 0, newstr, str1.length, str2.length);
        return newstr;
    }

    public static int[] arrayAddArray(int[] str1, int[] str2) {
        if (str1 == null) {
            return str2;
        }
        if (str2 == null) {
            return str1;
        }

        int[] newint = new int[str1.length + str2.length];
        System.arraycopy(str1, 0, newint, 0, str1.length);
        System.arraycopy(str2, 0, newint, str1.length, str2.length);
        return newint;
    }

    public static int[] arraySubArray(int[] str1, int[] str2) {
        if (str1 == null) {
            return new int[0];
        }
        if (str2 == null) {
            return str1;
        }

        int[] ret = new int[str1.length];
        int count = 0;
        int j;
        for (int i = 0; i < str1.length; ++i) {
            for (j = 0; j < str2.length; ++j) {
                if (str1[i] == str2[j]) {
                    break;
                }
            }
            if (j == str2.length) {
                ret[count++] = str1[i];
            }
        }
        int[] rets = new int[count];
        System.arraycopy(ret, 0, rets, 0, count);
        return rets;
    }


    public static int[] arrayToSort(int[] str1, int direct, int single) {
        if (str1 == null) {
            return new int[0];
        }

        for (int i = 0; i < str1.length; ++i) {
            int t;
            for (int j = 0; j < str1.length - 1 - i; ++j) {
                if (direct == 1) {
                    if (str1[j] < str1[j + 1]) {
                        t = str1[j];
                        str1[j] = str1[j + 1];
                        str1[j + 1] = t;
                    } else if (str1[j] == str1[j + 1] && single != 0) {
                        str1[j] = single;
                    }
                } else {
                    if (str1[j] > str1[j + 1]) {
                        t = str1[j];
                        str1[j] = str1[j + 1];
                        str1[j + 1] = t;
                    } else if (str1[j] == str1[j + 1] && single != 0) {
                        str1[j] = single;
                    }
                }
            }
        }
        return str1;
    }

    public static String rectifyOutput(String rtfy) {
        if (rtfy == null) {
            return "";
        } else {
            return rtfy.replaceAll(forRegex("\r\n"), "  ").replaceAll(forRegex("\\"), "\\\\");
        }
    }

    public static String getFileExname(String filepath) {
        if (filepath == null) {
            return "";
        } else {
            return filepath.substring(filepath.lastIndexOf(".") + 1);
        }
    }

 
    public static String arrayToString(String[] arr, String flag) {
        if (arr == null || arr.length == 0) {
            return "";
        }

        StringBuffer sbuf = new StringBuffer();
        for (int i = 0; i < arr.length - 1; ++i) {
            sbuf.append(arr[i]).append(flag);
        }
        sbuf.append(arr[arr.length - 1]);
        return sbuf.toString();
    }

    public static String getSomeLengthString(String src, int l, String addstr) {
        if (src == null) {
            return "";
        }
        if (l <= 0 || src.length() <= l) {
            return src;
        }
        return new StringBuffer().append(src.substring(0, l)).append(addstr).toString();
    }

   
    public static String intToNLengthString(int n, int intvalue) {
        String stringvalue = String.valueOf(intvalue);
        if (n <= stringvalue.length()) {
            return stringvalue;
        } else {
            StringBuffer sbuf = new StringBuffer();
            for (int i = 0; i < (n - stringvalue.length()); ++i) {
                sbuf.append("0");
            }
            return sbuf.append(stringvalue).toString();
        }
    }
}